/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tarena.lbs.attach.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
//        // 2. 新增：本地文件路径映射（关键！）
//        // 匹配 URL 路径：/**（所有请求，因为网关转发后是 /admin/attach/日期/文件名，去掉前缀后是 /日期/文件名）
//        // 映射到本地路径：D://home/images/dev/lbs_attach/
        // 启用：本地文件路径映射（关键！）
        // 匹配 URL 路径：/admin/attach/**（对应前端拿到的 url 前缀）
        // 映射到本地路径：D://home/images/dev/lbs_attach/
        //定义前端访问的 URL 规则。表示所有以 /admin/attach/ 开头的请求（比如 http://localhost:19999/admin/attach/2025/11/13/xxx.png），
        // 都会被这个规则匹配。
        registry.addResourceHandler("/admin/attach/**")
      //定义本地文件系统的真实路径。表示当前端请求匹配上述 URL 规则时，
                // 系统会到本地路径 D://home/images/dev/lbs_attach/ 下寻找对应的文件。
                .addResourceLocations("file:D://home/images/dev/lbs_attach/");
//
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//            .allowedOriginPatterns("*")
//            .allowedMethods("*")
//            .allowedHeaders("*")
//            .allowCredentials(true)
//            .maxAge(3600);
//    }
}
